package com.cxh.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.cxh.entity.SysUser;

public class LoginInterceptor implements HandlerInterceptor{

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		HttpSession session = request.getSession();
		//查看用户登录情况
		SysUser user = (SysUser)session.getAttribute("user");
		//获取用户请求URL
	    String userUrl=request.getServletPath();///login/toLogin
	    if(userUrl.equals("/controller/login/login")
	    		||userUrl.equals("/controller/login/toLogin")
	    		||userUrl.equals("/controller/login/timeOut")){
	    	
	    }else if(user == null){
	    	request.getRequestDispatcher("/controller/login/timeOut").forward(request, response);
			return false;
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
	}

}
